package beginner;

public class chapter3 {

    public static void main(String[] args){
        cal2(10);
    }
    //sum_3复杂度 O(2n^2+2n+3) = O(n^2),描述算法执行效率和数据规模增长的变化趋势
    //总的时间复杂度就等于量级最大的那段代码的时间复杂度,O(1)+O(n)+O(n^2)结果为O(n^2)
    public static void cal1(int n){
        int sum_1 = 0;
        int p=1;
        for(; p <= 100; p++){
            sum_1 += p;
        }

        int sum_2 = 0;
        int q = 1;
        for(; q <= n; q++){
            sum_2 += q;
        }

        int sum_3=0;
        int i=1;
        int j=1;
        for(;i <= n; i++) {
            j = 1;
            for(;j <= n; j++) {
                sum_3 = sum_3 + i*j;
            }
        }
        System.out.println(sum_1 +  sum_2 + sum_3);
    }

    public static void cal2(int n){
        int ret = 0;
        int i = 1;
        for(; i<n; ++i) {//这里++i和i++无区别
            System.out.println(i);
            ret += f(i);
        }
        System.out.println(ret);
    }
    public static int f(int n) {
        int sum = 0;
        int i =  0;
        for(;i < n; ++i) {
            sum += i;
        }
        return sum;
    }
    //时间复杂度 O(log3 n) ,log3n 就等于 log32 * log2n
    //在采用大 O 标记复杂度的时候，可以忽略系数 O(log3 n) = O(log n)
    public static void cal3(int n) {
        int i = 1;
        while (i <= n) {
            i = i * 3;
        }
    }
    //空间复杂度，表示算法的存储空间与数据规模之间的增长关系
    //这段代码空间复杂度为O(n)
    public static void cal4(int n) {
        int i = 1;
        int [] a = new int[n];
        for(; i < n; i++) {
            a[i] = i * i;
        }
    }
}

